// miniprogram/pages/home/index/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: './user-unlogin.png',
    userInfo: null,
    studentInfo:null,
    isPublic:null,
    isUser: null,
    hasNewApply:false,
    isLogined:app.globalData.isLogined,
    myProfile: [
      { "desc": "我的简历", "id": "resume", "link": "/pages/home/my/vitae" },
      { "desc": "我的收藏", "id": "follow", "link": "/pages/home/my/follow" },
      { "desc": "我的投递", "id": "delivery", "link": "/pages/home/my/delivery" }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (!wx.cloud) {
      wx.redirectTo({
        url: '../chooseLib/chooseLib',
      })
      return
    }

    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          //调用登陆云函数获取Openid
          wx.cloud.callFunction({
            name: 'login',
            data: {},
            success: res => {
              app.globalData.openid = res.result.openid;
              console.log('[云函数] [login] user openid: ', app.globalData.openid)
              //判断该openid有无绑定学生用户，有则获取学生用户信息
              wx.request({
                url: app.globalData.url + "/user/login",
                data: {
                  openid: app.globalData.openid
                },
                success: res => {
                  var isUser = res.data.isUser;
                  app.globalData.isUser = isUser;
                  this.setData({
                    isUser: isUser
                  })
                  console.log(isUser)
                  if (isUser == 1) {
                    this.setData({
                      studentInfo: res.data.studentInfo,
                      isPublic:res.data.isPublic
                    })
                    var uniqueId = res.data.uniqueId;
                    app.globalData.uniqueId = uniqueId;
                    app.globalData.studentInfo = res.data.studentInfo
                    console.log(app.globalData.studentInfo)
                  }

                }
              })
            },
            fail: err => {
              console.error('[云函数] [login] 调用失败', err)
            }
          })
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              this.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo
              })
              app.globalData.userInfo = res.userInfo;
            }
          })
        }
      }
    })
  },

  /**
   * 自定义函数
   */
  onGetUserInfo: function (e) {
    //授权后获取用户信息UserInfo
    if (!this.logged && e.detail.userInfo) {
      this.setData({
        logged: true,
        avatarUrl: e.detail.userInfo.avatarUrl,
        userInfo: e.detail.userInfo
      })
      app.globalData.userInfo = e.detail.userInfo;
      console.log('[用户授权][nickName]: ',app.globalData.userInfo.nickName)
    }
    //第一次授权后判断该openid是否有该用户
    //调用登陆云函数获取Openid
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        app.globalData.openid = res.result.openid;
        console.log('[云函数] [login] user openid: ', app.globalData.openid)
        //判断该openid有无绑定学生用户，有则获取学生用户信息
        wx.request({
          url: app.globalData.url + "/user/login",
          data: {
            openid: app.globalData.openid
          },
          success: res => {
            var isUser = res.data.isUser;
            app.globalData.isUser = isUser;
            this.setData({
              isUser: isUser
            })
            console.log(isUser)
            if (isUser == 1) {
              this.setData({
                studentInfo: res.data.studentInfo
              })
              app.globalData.studentInfo = res.data.studentInfo
              var uniqueId = res.data.uniqueId;
              app.globalData.uniqueId = uniqueId;
              console.log(app.globalData.studentInfo)
            }

          }
        })
      },
      fail: err => {
        console.error('[云函数] [login] 调用失败', err)
      }
    })
  },

  onGetOpenid: function () {
    // 调用云函数，获取openid
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        console.log('[云函数] [login] user openid: ', res.result.openid)
        app.globalData.openid = res.result.openid
        // wx.navigateTo({
        //   url: '../userConsole/userConsole',
        // })
      },
      fail: err => {
        console.error('[云函数] [login] 调用失败', err)
        // wx.navigateTo({
        //   url: '../deployFunctions/deployFunctions',
        // })
      }
    })
  },

  onRegister: function(){
    wx.navigateTo({
      url: '../register/register',
    })
  },
  navigator: function navigator(e) {
    var that = this;
    that.setData({ link: e.currentTarget.dataset.link });
    console.log(that.data.link);
    wx.navigateTo({
      url: that.data.link
    });
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          //调用登陆云函数获取Openid
          wx.cloud.callFunction({
            name: 'login',
            data: {},
            success: res => {
              app.globalData.openid = res.result.openid;
              console.log('[云函数] [login] user openid: ', app.globalData.openid)
              //判断该openid有无绑定学生用户，有则获取学生用户信息
              wx.request({
                url: app.globalData.url + "/user/login",
                data: {
                  openid: app.globalData.openid
                },
                success: res => {
                  var isUser = res.data.isUser;
                  app.globalData.isUser = isUser;
                  this.setData({
                    isUser: isUser
                  })
                  console.log(isUser)
                  if (isUser == 1) {
                    this.setData({
                      studentInfo: res.data.studentInfo,
                      hasNewApply:res.data.hasNewApply,
                      isPublic: res.data.isPublic
                    })
                    app.globalData.studentInfo = res.data.studentInfo
                    var uniqueId = res.data.uniqueId;
                    app.globalData.uniqueId = uniqueId;
                  }
                },
              })
            },
            fail: err => {
              console.error('[云函数] [login] 调用失败', err)
            }
          })
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              this.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo
              })
              if (app.globalData.isUser == 1) {
                wx.request({
                  url: app.globalData.url + '/user/updateAvatar',
                  data: {
                    openid: app.globalData.openid,
                    avatarUrl: app.globalData.userInfo.avatarUrl
                  },
                  success: res => {
                    if (res.data.code == 0) {
                      wx.showModal({
                        title: '提示',
                        content: res.data.msg,
                      })
                    }
                  }
                })
              }
              app.globalData.userInfo = res.userInfo;
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})